package com.zxl.yjb.fragment.home.sharemessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxl.yjb.R;
import com.zxl.yjb.activity.home.sharemessage.MessageListActivity;
import com.zxl.yjb.activity.home.sharemessage.ShareMessageDetailActivity;
import com.zxl.yjb.adapter.ShareMsgAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.LazyBaseFragment;
import com.zxl.yjb.bean.LocationBean;
import com.zxl.yjb.bean.ShareMsgBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public abstract class ShareMessageBaseFragment extends LazyBaseFragment {

    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.sm_refresh)
    SmartRefreshLayout sm_refresh;

    private int page = 1;
    private int count = 10;

    private MessageListActivity activity;
    private LinearLayoutManager linearLayoutManager;
    private ShareMsgAdapter shareMsgAdapter;
    private ArrayList<ShareMsgBean> shareMsgBeans = new ArrayList<>();
    public LocationBean locationBean;

    private DataManager dataManager;

    private String country = "";
    private String province = "";
    private String city = "";
    private String district = "";
    private String street = "";
    private double lng;
    private double lat;

    private int id;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_share_message;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        activity = (MessageListActivity) mActivity;
        id = activity.getIntent().getIntExtra("id",0);
        locationBean = (LocationBean) SaveBeanUtil.getBean(getActivity(), Const.LOCATION_LNGLAT);

        if (locationBean == null)
            locationBean = new LocationBean();
      country = getCountry();
       /*   province = getProvince();
        city = getCity();
        district = getDistrict();
        street = getStreet();*/
        lng = locationBean.getLng();
        lat = locationBean.getLat();

        linearLayoutManager = new LinearLayoutManager(getActivity());
        rv_list.setLayoutManager(linearLayoutManager);
        shareMsgAdapter = new ShareMsgAdapter(R.layout.iten_share_msg,shareMsgBeans);
        rv_list.setAdapter(shareMsgAdapter);
        shareMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ShareMessageDetailActivity.class);
                intent.putExtra("id",shareMsgBeans.get(position).getId());
                getActivity().startActivity(intent);
            }
        });

        sm_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getShareMsgList();

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getShareMsgList();
            }
        });

        getShareMsgList();
    }

    @Override
    protected void main() {

    }


    public abstract String getCountry();
    public abstract String getProvince();
    public abstract String getCity();
    public abstract String getDistrict();
    public abstract String getStreet();

    public String getAdType(){
        return "";
    };


    private void getShareMsgList() {
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("count",count+"");
        map.put("listid",id+"");
        map.put("country",country);
        map.put("province",province);
        map.put("city",city);
        map.put("district",district);
        map.put("street",street);
        map.put("lon",lng+"");
        map.put("lat",lat+"");
        ObserverOnNextListener<ArrayList<ShareMsgBean>> listener = new ObserverOnNextListener<ArrayList<ShareMsgBean>>() {
            @Override
            public void onNext(ArrayList<ShareMsgBean> shareMsgBeansList) {
                if (page == 1)
                    shareMsgBeans.clear();
                shareMsgBeans.addAll(shareMsgBeansList);
                shareMsgAdapter.notifyDataSetChanged();

                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        ProgressObserver<ArrayList<ShareMsgBean>> observer = new ProgressObserver<ArrayList<ShareMsgBean>>(listener, getActivity()) {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        if (!TextUtils.isEmpty(getAdType()) && getAdType().equals("sheng")) {
            dataManager.getSharemsgList(observer,map);
        }else {
            dataManager.getClaList(observer,map);
        }

    }

    public void refreshShareMsgList(String provinceSelect, String citySelect, String countySelect, String streetSelect) {
        this.street = streetSelect;
        this.district = countySelect;
        this.city = citySelect;
        this.province = provinceSelect;
        if (TextUtils.isEmpty(country)) {
            return;
        }
        page = 0;
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("count",count+"");
        map.put("listid",id+"");
        map.put("country",country);
        map.put("province", provinceSelect);
        map.put("city", citySelect);
        map.put("district", countySelect);
        map.put("street", streetSelect);
        map.put("lon",lng+"");
        map.put("lat",lat+"");
        ObserverOnNextListener<ArrayList<ShareMsgBean>> listener = new ObserverOnNextListener<ArrayList<ShareMsgBean>>() {
            @Override
            public void onNext(ArrayList<ShareMsgBean> shareMsgBeansList) {
                shareMsgBeans.clear();
                shareMsgBeans.addAll(shareMsgBeansList);
                shareMsgAdapter.notifyDataSetChanged();

                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        ProgressObserver<ArrayList<ShareMsgBean>> observer = new ProgressObserver<ArrayList<ShareMsgBean>>(listener, getActivity()) {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        if (!TextUtils.isEmpty(getAdType()) && getAdType().equals("sheng")) {

            dataManager.getSharemsgList(observer,map);
        }else {

            dataManager.getClaList(observer,map);
        }

    }


}
